{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Representing a Vector\n",
    "\n",
    "In nengo, ensembles of neurons represent information. In this model, you will represent a two-dimensional vector with a single ensemble of LIF (leaky integrate-and-fire) neurons. Though this is a case of two-dimensional vector representation, but the ideas naturally generalize to any dimension."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Setup the envirnment\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "%matplotlib inline\n",
    "\n",
    "import nengo\n",
    "from nengo.dists import Uniform"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create the Model\n",
    "\n",
    "This model has paramenters as described in the book, with the neurons in the ensemble having two dimensions. Since the default max rates in Nengo 2.0 are (200, 400), so you have to explicitly specify them to be (100, 200) to create the same model as in the book. The default values of tauRC, tauRef, intercepts, radius and expected noise in Nengo 2.0 are same as those mentioned in the book."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = nengo.Network(label='2D Representation')\n",
    "with model:\n",
    "    # Input Nodes\n",
    "    sin = nengo.Node(output=np.sin)\n",
    "    cos = nengo.Node(output=np.cos)\n",
    "    \n",
    "    # Ensemble with 100 LIF neurons which represents a 2-dimensional signal\n",
    "    x = nengo.Ensemble(100, dimensions=2, max_rates=Uniform(100, 200))\n",
    "    \n",
    "    # Get the neuron encoders\n",
    "    encoders = x.encoders.sample(100,2)\n",
    "    \n",
    "    # Connecnting input to ensemble\n",
    "    # The indices in ensemble 'x' define which dimension the input will project to\n",
    "    nengo.Connection(sin, x[0])\n",
    "    nengo.Connection(cos, x[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add Probes to Collect Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with model:\n",
    "    sin_probe = nengo.Probe(sin)                   \n",
    "    cos_probe = nengo.Probe(cos)                   \n",
    "    x_probe = nengo.Probe(x, synapse=0.01)  # Decoded ouput of the ensemble"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run the model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import the nengo_gui visualizer to run and visualize the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from nengo_gui.ipython import IPythonViz\n",
    "IPythonViz(model, \"ch2-vectors.py.cfg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Press the play button in the visualizer to run the simulation. You should see the graphs as shown in the figure below.\n",
    "\n",
    "The `cos` and `sin` graphs show the two dimensional input provided to the ensemble and the top right graph shows the the decoded estimate of this two dimensional input. The graph on bottom right shows the XY-value i.e., the state represented by one dimension of the ensemble vs the state represented by the other dimension. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from IPython.display import Image\n",
    "Image(filename='ch2-vectors.png')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
